package com.fmsz.physicalheadline.activitys;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.fmsz.physicalheadline.R;
import com.fmsz.physicalheadline.databinding.ActivityBaseBinding;
import com.fmsz.physicalheadline.fragments.BaseEventFragment;
import com.fmsz.physicalheadline.fragments.BaseHeadlineFragment;
import com.fmsz.physicalheadline.fragments.BaseNewsFragment;
import com.fmsz.physicalheadline.interfaces.SlidingOpen;

public class BaseActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener, SlidingPaneLayout.PanelSlideListener, NavigationView.OnNavigationItemSelectedListener, SlidingOpen {

    private int prePosition = 1;
    private Fragment[] fgtArr;

    private ActivityBaseBinding binding;
    private boolean backSecond;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding =  DataBindingUtil.setContentView(this, R.layout.activity_base);
        initView();
    }

    //实例化布局
    private void initView() {
        fgtArr = new Fragment[]{BaseNewsFragment.newInstance(), BaseHeadlineFragment.newInstance(), BaseEventFragment.newInstance()};
        getSupportFragmentManager().beginTransaction().add(R.id.base_fgt, fgtArr[1]).commit();
        changeFgt(1);
        binding.baseRg.setOnCheckedChangeListener(this);
        binding.slidingPaneLayout.setSliderFadeColor(0);
        binding.slidingPaneLayout.setPanelSlideListener(this);
        binding.navigationView.setItemIconTintList(null);
        View headerView = binding.navigationView.getHeaderView(0);
        ImageView imageView = (ImageView) headerView.findViewById(R.id.default_head);
        imageView.setOnClickListener(this);
        binding.navigationView.setNavigationItemSelectedListener(this);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.base_news:
                changeFgt(0);
                break;
            case R.id.base_headline:
                changeFgt(1);
                break;
            case R.id.base_event:
                changeFgt(2);
                break;
        }
    }

    private void changeFgt(int curPosition) {
        if (curPosition == prePosition) {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(fgtArr[prePosition]);
        if (fgtArr[curPosition].isAdded()) {
            transaction.show(fgtArr[curPosition]);
        } else {
            transaction.add(R.id.base_fgt, fgtArr[curPosition]).show(fgtArr[curPosition]);
        }
        transaction.commit();
        prePosition = curPosition;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this,LoginActivity.class);
        startActivity(intent);
    }

    @Override
    public void onPanelSlide(View panel, float slideOffset) {

    }

    @Override
    public void onPanelOpened(View panel) {

    }

    @Override
    public void onPanelClosed(View panel) {

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.community:
                Toast.makeText(this, "点击社区了", Toast.LENGTH_SHORT).show();
                break;
            case R.id.message:
                Toast.makeText(this, "点击消息了", Toast.LENGTH_SHORT).show();
                break;
            case R.id.subscribe:
                Toast.makeText(this, "点击订阅了", Toast.LENGTH_SHORT).show();
                break;
            case R.id.collection:
                Toast.makeText(this, "点击收藏了", Toast.LENGTH_SHORT).show();
                break;
            case R.id.feedback:
                Toast.makeText(this, "点击反馈了", Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting:
                Toast.makeText(this, "点击设置了", Toast.LENGTH_SHORT).show();
                break;
        }

        return true;
    }

    @Override
    public void isOpenOrClose() {
        if (binding.slidingPaneLayout.isOpen()) {
            binding.slidingPaneLayout.closePane();
        } else {
            binding.slidingPaneLayout.openPane();
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.alpha = 0.7f;
            getWindow().setAttributes(params);
        }
    }

    @Override
    public void onBackPressed() {
        if (backSecond) {
            super.onBackPressed();
        }else{
            backThread();
            Toast.makeText(this, "再次点击退出程序", Toast.LENGTH_SHORT).show();
        }
    }

    private void backThread() {
        backSecond = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3*1000);
                    backSecond = false;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        binding.baseRg.removeCallbacks()
    }
}
